package com.gzhryc.shared_device.bxscn.code.services;

import com.gzhryc.common.StringTools;
import com.gzhryc.common.logging.Logger;
import com.gzhryc.jdbc.Conditions;
import com.gzhryc.jdbc.MultiDBTools;
import com.gzhryc.shared_device.bxscn.code.dao.BXSCNBatteryTypeDayCostDao;
import com.gzhryc.shared_device.bxscn.code.dao.db.BXSCNBatteryTypeDayCost;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;

public class BXSCNBatteryTypeDayCostService {

    static Logger log = Logger.getLogger(BXSCNBatteryTypeDayCostService.class);

    public static BXSCNBatteryTypeDayCostService self() {
        return MultiDBTools.getService(BXSCNBatteryTypeDayCostService.class, true);
    }

    public static BXSCNBatteryTypeDayCostService self(String jdbcKey) {
        return MultiDBTools.getService(jdbcKey, BXSCNBatteryTypeDayCostService.class, true);
    }

    BXSCNBatteryTypeDayCostDao dao;

    public BXSCNBatteryTypeDayCostService(String jdbcKey) {
        this.dao = new BXSCNBatteryTypeDayCostDao(jdbcKey);
    }

    public List<BXSCNBatteryTypeDayCost> findJoinAll(){
    	try {
			return dao.findJoinAll();
		} catch (SQLException e) {
			log.error(e.getMessage(),e);
		}
    	return null;
    }
    
    public Long count(){
        try {
            return dao.count();
        } catch (SQLException e) {
            log.error(e.getMessage(),e);
        }
        return 0L;
    }

    public BXSCNBatteryTypeDayCost getByTypeId(String typeId){
        if(StringTools.isNotBlank(typeId)){
            try {
                return dao.getById(typeId);
            } catch (SQLException e) {
                log.error(e.getMessage(),e);
            }
        }
        return null;
    }

    public List<BXSCNBatteryTypeDayCost> findByTypeIds(List<String> typeIds){
        if(typeIds != null && typeIds.size() > 0){
            Conditions conditions = new Conditions(BXSCNBatteryTypeDayCost.class);
            conditions.and().in("typeId",typeIds);

            try {
                return dao.findByCondition(conditions);
            } catch (SQLException e) {
                log.error(e.getMessage(),e);
            }
        }
        return null;
    }

    public boolean save(BXSCNBatteryTypeDayCost bxscnBatteryTypeDayCost){
        BXSCNBatteryTypeDayCost temp = getByTypeId(bxscnBatteryTypeDayCost.getTypeId());
        if(temp != null){
            try {
                return dao.updateNotNull(bxscnBatteryTypeDayCost) > 0;
            } catch (SQLException e) {
                log.error(e.getMessage(),e);
            }
        }else {
            bxscnBatteryTypeDayCost.setCreateDate(new Date());
            try {
                return dao.insert(bxscnBatteryTypeDayCost) > 0;
            } catch (SQLException e) {
                log.error(e.getMessage(), e);
            }
        }
        return false;
    }

    public boolean deleteById(String typeId){
        if(StringTools.isNotBlank(typeId)){
            try {
                return dao.deleteById(typeId) > 0;
            } catch (SQLException e) {
                log.error(e.getMessage(),e);
            }
        }
        return false;
    }
}
